const express = require('express')
const router = express.Router()
const mysql = require('mysql')
const moment = require('moment');
const path = require("path")

let db = mysql.createPool({
    host: 'localhost',
    user: 'root',
    password: 'root',
    database: 'byy'
});

router.use((req, res, next) => {
    console.log('进入user', moment().format())
    next()
})

router.get("/test",(req,res)=>{
    console.log("success")
})

router.get('/admin/:name',(req,res)=>{
    res.sendFile(path.join(__dirname,'../page/', req.params.name+'.html'))
})

router.get('/allUse',(req,res)=>{
    db.query(`select * from user`,(err,data)=>{
        res.json(data)
    })
})


router.get('/register',(req,res)=>{
    db.query(`insert into user (username,password,name) value('${req.query.username}','${req.query.password}','${req.query.name}')`, (err, data) => {
        if (err) {
            console.log(err)
        } else {
            db.query(`SELECT * FROM user where username='${req.query.username}'`, (err, data) => {
                if (err) {
                    console.log(err)
                } else {
                    console.log(data)
                    res.json({
                        code: 1,
                        data: data
                    })
                }
            })
        }
    })
})

router.get('/login', (req, res) => {
    console.log(req.query)
    db.query(`SELECT * FROM user where username='${req.query.username}' and password = '${req.query.password}'`, (err, data) => {
        if (err) {
            console.log(err)
        } else {
            console.log(data)
            res.json({
                code: 1,
                data: data
            })
        }
    })
})

router.get('/userAll', (req, res) => {
    console.log(req.query)
    db.query(`SELECT * FROM user`, (err, data) => {
        if (err) {
            console.log(err)
        } else {
            var newData = {
                code: 0,
                msg: '',
                count: data.length,
                "data": data
            }
            console.log(newData)
            res.json(newData)
        }
    })
})

router.get('/fileAll', (req, res) => {
    console.log(req.query)
    db.query(`SELECT * FROM file`, (err, data) => {
        if (err) {
            console.log(err)
        } else {
            var newData = {
                code: 0,
                msg: '',
                count: data.length,
                "data": data
            }
            console.log(newData)
            res.json(newData)
        }
    })
})

router.get('/updataName', (req, res) => {
    db.query(`update user set name = ${req.query.name} where username = ${req.query.username}`, (err, data) => {
        if (err) {
            console.log(err)
        } else {
            console.log(data)
            res.json({
                code: 1,
                data: data
            })
        }
    })
})

router.get('/deleteUser', (req, res) => {
    db.query(`delete from user where username=${req.query.username}`, (err, data) => {
        if (err) {
            console.log(err)
        } else {
            console.log(data)
            res.json({
                code: 1,
            })
        }
    })
})

router.get('/deleteUser', (req, res) => {
    db.query(`delete from user where id=${req.query.id}`, (err, data) => {
        if (err) {
            console.log(err)
        } else {
            console.log(data)
            res.json({
                code: 1,
            })
        }
    })
})

router.get('/deleteFile', (req, res) => {
    db.query(`delete from user where id=${req.query.id}`, (err, data) => {
        if (err) {
            console.log(err)
        } else {
            console.log(data)
            res.json({
                code: 1,
            })
        }
    })
})


router.post('/loginSuper', (req, res) => {
    console.log(req.query)
    db.query(`SELECT * FROM super_user where username='${req.query.username}' and password = '${req.query.password}'`, (err, data) => {
        if (err) {
            console.log(err)
        } else {
            console.log(data)
            res.json({
                username: req.query.username
            })
        }
    })
})


module.exports = router